/* 
 * File:   SocketAsincronico.h
 * Author: max
 *
 * Created on June 1, 2013, 9:07 AM
 */


#ifndef SOCKETASINCRONICO_H
#define	SOCKETASINCRONICO_H

#include <iostream>
#include <queue>
#include <string>
#include "common_Thread.h"
#include "client_Socket.h"
#include "common_Excepcion.h"
#include "common_Mutex.h"

using namespace std;
using namespace TP;

namespace TP { namespace Comunicacion {
    
class SocketAsincronico : public Thread {

public:

    SocketAsincronico (const string& pIp, const string& pPuerto);

    SocketAsincronico (const string& pIp, const string& pPuerto, const int &pTimeout);

    virtual ~SocketAsincronico ();

    void Iniciar() throw (Excepcion&);

    void Finalizar() throw (Excepcion&);

    int Escribir(const uint8_t* pMensaje, const size_t pLongitud) throw (Excepcion&);

    int Leer (uint8_t* &pMensaje, uint32_t &pLongitud) throw (Excepcion&);

protected:
    virtual void tarea ();
    
private:
    
    queue<string*>& bufferIn();
    
    queue<string*>& bufferOut();

    bool GetTransmitiendo();
    
    bool SetTransmitiendo(bool pValor);
    
    Socket* _socket;
    bool _transmitiendo;
    queue<string*> _bufferIn;
    queue<string*>   _bufferOut;
    Mutex _mutexBufferIn
        , _mutexBufferOut
        , _mutexTransmitiendo;
};

}}
#endif	/* SOCKETASINCRONICO_H */

